package com.example.testlistview;

import java.util.ArrayList;
import java.util.Arrays;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	public ListView mainListView;
	public String[] values;

	private static final int MAIN_ACTIVITY = 0;

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.activity_main);

		// Find the ListView resource.
		values = getResources().getStringArray(R.array.names);
		mainListView = (ListView) findViewById(R.id.mainListView);
		mainListView.setAdapter(new CustomArrayAdapter(this, values));
		final Intent intent = new Intent(this, DetailActivity.class);

		mainListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				/*
				 * Toast.makeText(getApplicationContext(), "Click ListItem " +
				 * values[position], Toast.LENGTH_LONG) .show();
				 */
				intent.putExtra("item", values[position]);
				startActivityForResult(intent, MAIN_ACTIVITY);
				// startActivity(intent);
			}
		});

		registerForContextMenu(mainListView);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		Toast.makeText(getApplicationContext(),
				"return from our DetailActivity ", Toast.LENGTH_LONG).show();
		// check which activity gives data
		switch (requestCode) {
		// checking for our DetailActivity using request code
		case MAIN_ACTIVITY:
			// check whether result comes with RESULT_OK (That mean no problem
			// in result)
			if (resultCode == RESULT_OK) {
				// then get the action string that return from our
				// DetailActivity
				String action = data.getExtras().getString("action");
				// then set footer message

				TextView text = (TextView) findViewById(R.id.footer);
				text.setText(String.format("Returned %s forom detail activity",
						action));
			}
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.mainListView) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			menu.setHeaderTitle(values[info.position]);
			String[] menuItems = getResources().getStringArray(R.array.menu);
			for (int i = 0; i < menuItems.length; i++) {
				menu.add(Menu.NONE, i, i, menuItems[i]);
			}
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int menuItemIndex = item.getItemId();
		String[] menuItems = getResources().getStringArray(R.array.menu);
		String menuItemName = menuItems[menuItemIndex];
		String listItemName = values[info.position];

		TextView text = (TextView) findViewById(R.id.footer);
		text.setText(String.format("Selected %s for item %s", menuItemName,
				listItemName));
		return true;
	}

}
/*
 * public class MainActivity extends ListActivity {
 * 
 * protected Object mActionMode; public int selectedItem = -1;
 * 
 * @Override public void onCreate(Bundle savedInstanceState) {
 * super.onCreate(savedInstanceState);
 * 
 * setListAdapter(new CustomArrayAdapter(this,
 * getResources().getStringArray(R.array.names)));
 * registerForContextMenu(getListView()); }
 * 
 * @Override protected void onListItemClick(ListView l, View v, int position,
 * long id) {
 * 
 * //get selected items String selectedValue = (String)
 * getListAdapter().getItem(position); Toast.makeText(this, selectedValue,
 * Toast.LENGTH_SHORT).show();
 * 
 * }
 * 
 * @Override public void onCreateContextMenu(ContextMenu menu, View v,
 * ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo);
 * MenuInflater inflater = getMenuInflater();
 * inflater.inflate(R.menu.activity_main, menu); }
 * 
 * @Override public boolean onContextItemSelected(MenuItem item) {
 * AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
 * String[] names = getResources().getStringArray(R.array.names);
 * switch(item.getItemId()) { case R.id.edit: Toast.makeText(this,
 * "You have chosen the " + getResources().getString(R.string.edit) +
 * " context menu option for " + names[(int)info.id],
 * Toast.LENGTH_SHORT).show(); return true; case R.id.delete:
 * Toast.makeText(this, "You have chosen the " +
 * getResources().getString(R.string.delete) + " context menu option for " +
 * names[(int)info.id], Toast.LENGTH_SHORT).show(); return true; case R.id.view:
 * Toast.makeText(this, "You have chosen the " +
 * getResources().getString(R.string.view) + " context menu option for " +
 * names[(int)info.id], Toast.LENGTH_SHORT).show(); return true; default: return
 * super.onContextItemSelected(item); } }
 * 
 * }
 */